package cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.dis.controller.admin.disgoods;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.dis.controller.admin.disgoods.vo.DisGoodsCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.dis.controller.admin.disgoods.vo.DisGoodsExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.dis.controller.admin.disgoods.vo.DisGoodsPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.dis.controller.admin.disgoods.vo.DisGoodsUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.dis.dal.dataobject.disgoods.DisGoodsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.dis.dal.mysql.disgoods.DisGoodsMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.dis.service.disgoods.DisGoodsService;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.controller.admin.goods.vo.GoodSpecif;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.controller.admin.goods.vo.GoodSpecifChild;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.dataobject.goods.GoodsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.mysql.goods.GoodsMapper;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 折扣商品")
@RestController
@RequestMapping("/qd/dis-goods")
@Validated
public class DisGoodsController {

    @Resource
    private DisGoodsService disGoodsService;
    @Resource
    private DisGoodsMapper disGoodsMapper;
    @Resource
    GoodsMapper goodsMapper;

    @GetMapping("getList")
    public CommonResult getList(Integer goodsId){
        List<DisGoodsDO> disGoodsDOS = new ArrayList<>();
        if (goodsId!=null){
            disGoodsDOS=disGoodsMapper.selectList(new LambdaQueryWrapperX<DisGoodsDO>().eq(DisGoodsDO::getGoodsId,goodsId));
            for (DisGoodsDO vo : disGoodsDOS) {
                List<GoodSpecif> specif = JSON.parseArray(vo.getGoodsInfo().toString(),GoodSpecif.class);
                if (specif!=null&&specif.size()>0){
                    List<GoodSpecifChild> child = specif.get(0).getChild();
                    if (child!=null&&child.size()>0){
                        vo.setInitPrice(child.get(0).getPrice());
                        vo.setDisPrice(child.get(0).getGroupPrice());
                    }
                }
            }
        }


        return success(disGoodsDOS);
    }
    //启用
    @GetMapping("openDis")
    public CommonResult openDis(Integer id){
        DisGoodsDO disGoodsDO = disGoodsMapper.selectById(id);
        if (disGoodsDO==null)
            throw new ServiceException(500,"数据不存在");
        if (LocalDateTime.now().compareTo(disGoodsDO.getEntTime())>0)
            throw new ServiceException(500,"已过折扣时间");
        List<DisGoodsDO> disGoodsDOS =disGoodsMapper.selectList(new LambdaQueryWrapperX<DisGoodsDO>().eq(DisGoodsDO::getGoodsId,disGoodsDO.getGoodsId()).eq(DisGoodsDO::getStatus,1));
        if (disGoodsDOS.size()>0)
            throw new ServiceException(500,"其他折扣策略已生效,请先下架别的");

        GoodsDO goodsDO = goodsMapper.selectById(disGoodsDO.getGoodsId());
        if (goodsDO==null)
            throw new ServiceException(500,"商品不存在");
        goodsDO.setDisId(disGoodsDO.getId());
        goodsDO.setIsDisGoods(1);
        goodsDO.setDisSpecif(disGoodsDO.getGoodsInfo());
        goodsMapper.updateById(goodsDO);

        disGoodsDO.setStatus(1);
        disGoodsMapper.updateById(disGoodsDO);

        return success("");
    }
    //禁用
    @GetMapping("offDis")
    public CommonResult offDis(Integer id){
        DisGoodsDO disGoodsDO = disGoodsMapper.selectById(id);
        if (disGoodsDO==null)
            throw new ServiceException(500,"数据不存在");

        GoodsDO goodsDO = goodsMapper.selectById(disGoodsDO.getGoodsId());
        if (goodsDO==null)
            throw new ServiceException(500,"商品不存在");
        goodsDO.setDisId(null);
        goodsDO.setIsDisGoods(2);
        goodsDO.setDisSpecif(new ArrayList<>());
        goodsMapper.updateById(goodsDO);

        disGoodsDO.setStatus(2);
        disGoodsMapper.updateById(disGoodsDO);
        return success("");
    }

    @PostMapping("/create")
    @Operation(summary = "创建折扣商品")
    @PreAuthorize("@ss.hasPermission('qd:dis-goods:create')")
    public CommonResult<Integer> createDisGoods(@Valid @RequestBody DisGoodsDO createReqVO) {
        return success(disGoodsService.createDisGoods(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新折扣商品")
    @PreAuthorize("@ss.hasPermission('qd:dis-goods:update')")
    public CommonResult<Boolean> updateDisGoods(@Valid @RequestBody DisGoodsDO updateReqVO) {
        disGoodsService.updateDisGoods(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除折扣商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qd:dis-goods:delete')")
    public CommonResult<Boolean> deleteDisGoods(@RequestParam("id") Integer id) {
        disGoodsService.deleteDisGoods(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得折扣商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:dis-goods:query')")
    public CommonResult getDisGoods(@RequestParam("id") Integer id) {
        return success( disGoodsService.getDisGoods(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得折扣商品列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qd:dis-goods:query')")
    public CommonResult getDisGoodsList(@RequestParam("ids") Collection<Integer> ids) {
        return success(disGoodsService.getDisGoodsList(ids));
    }

    @GetMapping("/page")
    @Operation(summary = "获得折扣商品分页")
    @PreAuthorize("@ss.hasPermission('qd:dis-goods:query')")
    public CommonResult getDisGoodsPage(@Valid DisGoodsPageReqVO pageVO) {
        return success(disGoodsService.getDisGoodsPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出折扣商品 Excel")
    @PreAuthorize("@ss.hasPermission('qd:dis-goods:export')")
    @OperateLog(type = EXPORT)
    public void exportDisGoodsExcel(@Valid DisGoodsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
//        List<DisGoodsDO> list = disGoodsService.getDisGoodsList(exportReqVO);
//         导出 Excel
//        List<DisGoodsExcelVO> datas = DisGoodsConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "折扣商品.xls", "数据", DisGoodsExcelVO.class, datas);
    }

}
